package com.shanpin.shebao.admin.module.keyvalue;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.shanpin.shebao.admin.db.AppDbRepository;

@Repository
public class KeyValueDao {

	@Autowired
	private AppDbRepository appDB;
	
	/**
	 * 版本控制相关的数据
	 * @return
	 */
	List<KeyValuePojo> version() {
		String sql = "SELECT key_name, key_value FROM t_sb_keyvalue WHERE key_name LIKE 'version%' ORDER BY order_id";
		return appDB.query(sql, KeyValuePojo.viewMapper);
	}
	

	public String get(String key_name) {
		String sql = "SELECT key_value FROM t_sb_keyvalue WHERE key_name=? LIMIT 1";
		return appDB.queryString(sql, key_name);
	}
	
	public boolean add(String key_name, String key_value) {
		Date curDate = new Date();
		String sql = "INSERT INTO t_sb_keyvalue (key_name, key_value, update_time) values (?, ?, ?)";
		return appDB.update(sql, key_name, key_value, curDate);
	}
	
	public boolean update(String key_name, String key_value) {
		String sql = "INSERT INTO t_sb_keyvalue (key_name, key_value, update_time) values (?, ?, NOW()) "
				+ " ON DUPLICATE KEY UPDATE key_value=?, update_time=NOW()";
		return appDB.update(sql, key_name, key_value, key_value);
	}
}
